//
//  SliderMenuView.swift
//  VO_Yuejie
//
//  Created by laichunhui on 15/11/20.
//  Copyright © 2015年 lch. All rights reserved.
//

import UIKit
import SnapKit

class SliderMenuView: UIView {
    /// 定时器
    lazy var playLink: CADisplayLink = {
        let link = CADisplayLink(target: self, selector: "startRotating")
//      [self.link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
        return link
    }()
    
    /// innerCycle
    lazy var innerCycle = UIImageView(image: UIImage(named: "slider_inner_cycle"))
    
    /// outerCycle
    lazy var outerCycle = UIImageView(image: UIImage(named: "slider_outer_cycle"))
    
    /// menuView
    lazy var menuView = UIView()
    
    /// 分类按钮
    var catButtons = NSMutableArray()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func willMoveToSuperview(newSuperview: UIView?) {
        
        layoutUI()
        // 设置按钮
        setupMenuButton()
    }
    
    override func didMoveToSuperview() {
        // 开始定时器
        self.playLink .addToRunLoop(NSRunLoop.mainRunLoop(), forMode: NSDefaultRunLoopMode)
    }
    
    func startRotating() {
        innerCycle.transform = CGAffineTransformRotate(innerCycle.transform, CGFloat(M_PI / 500.0))
        outerCycle.transform = CGAffineTransformRotate(outerCycle.transform, -CGFloat(M_PI / 600.0))
        
        for btn in catButtons {
        
            if btn.isKindOfClass(UIButton.self) {
                let button = btn as! UIButton
                button.transform = CGAffineTransformRotate(button.transform, -CGFloat(M_PI / 500.0))
            }
        }
    }

    /// 设置按钮
    private func setupMenuButton() {
        
        innerCycle.userInteractionEnabled = true
        let categorys = ["大家", "新知", "生活", "格调", "摩登"]
        
        for i:Int in 0...4 {
            /* 按钮背景 */
            let btnBg = UIView()
            btnBg.bounds = CGRectMake(0, 0, 62, 124) //outerCycle.height * 0.5)
            // 设置锚点和位置
            btnBg.layer.anchorPoint = CGPointMake(0.5, 1)
            btnBg.layer.position = CGPointMake(innerCycle.width * 0.5, innerCycle.height * 0.5)
           // btnBg.y = 20
            // 设置旋转角度
            let angle = CGFloat(i) * CGFloat((72.0 / 180.0) * M_PI)
            btnBg.transform = CGAffineTransformMakeRotation(angle)
            
            innerCycle.addSubview(btnBg)
            
            /* 按钮 */
            let btn = UIButton(type: .Custom)
            btn.setBackgroundImage(UIImage(named: "slider_menu_btn"), forState: .Normal)
            btn.setTitle(categorys[i], forState: .Normal)
            btn.frame = CGRectMake(0, 0, 62, 62) //outerCycle.height * 0.5)
            btn.addTarget(self, action: "selectCategory:", forControlEvents: .TouchUpInside)
            // 设置旋转角度, 摆正文字
            btn.transform = CGAffineTransformMakeRotation(-angle)
  
            catButtons.addObject(btn)
            
            btnBg.addSubview(btn)
        }
    }
    
    func selectCategory(btn: UIButton) {
        print(btn.titleLabel?.text)
    }
    
    //MARK: 布局视图
    private func layoutUI() {
        
        // view.insertSubview(bgView, atIndex: 0)
        self.addSubview(innerCycle)
        
        self.addSubview(outerCycle)
        
        self.addSubview(menuView)
        
        
        //自动布局
        innerCycle.snp_makeConstraints(closure: { (make) -> Void in
            make.edges.equalTo(self)
        })
        
        outerCycle.snp_makeConstraints(closure: { (make) -> Void in
            make.edges.equalTo(self)
        })
        
        innerCycle.snp_makeConstraints(closure: { (make) -> Void in
            make.edges.equalTo(self)
        })
    }
    
    deinit {
        playLink.invalidate()
    }
    
}
